package com.innazis.jfrogsurvey.backendmanager;

import com.innazis.jfrogsurvey.config.Constants;
import com.innazis.jfrogsurvey.obj.AnswerObj;
import com.innazis.jfrogsurvey.obj.QuestionObj;
import com.innazis.jfrogsurvey.obj.SurveyObj;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Inna on 04.06.14.
 */
public final class ParameterUtility {

    public static JSONObject CreateAnswerParamsJSON(AnswerObj answerObj) {
        JSONObject json = new JSONObject();
        try {
            json.put(Constants.answer_id_p,answerObj.get_answer_id());
            json.put(Constants.question_id_p,answerObj.get_question_id());
            json.put(Constants.survey_id_p,answerObj.get_survey_id());
            json.put(Constants.answer_order_p,answerObj.get_answer_order());
            json.put(Constants.answer_text_p,answerObj.get_answer_text());
            json.put(Constants.object_name_p,answerObj.get_object_name());
            json.put(Constants.is_checked_p, answerObj.is_is_checked());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public static JSONObject CreateQuestionParamsJSON(QuestionObj questionObj) {
        JSONObject json = new JSONObject();
        try {
            json.put(Constants.question_id_p,questionObj.get_question_id());
            json.put(Constants.survey_id_p,questionObj.get_survey_id());
            json.put(Constants.question_title_p,questionObj.get_question_title());
            json.put(Constants.question_order_p,questionObj.get_question_order());
            json.put(Constants.answers_num_p,questionObj.get_answers_num());

            JSONArray arrAnswers = new JSONArray();
            JSONObject tempObj = null;
            for (AnswerObj item : questionObj.get_answers_list()) {
                tempObj = new JSONObject();
                tempObj = CreateAnswerParamsJSON(item);
                arrAnswers.put(tempObj);
            }
            json.put(Constants.answers_list_p,arrAnswers);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public static JSONObject CreateSurveyParamsJSON(SurveyObj surveyObj) {
        JSONObject json = new JSONObject();
        try {
            json.put(Constants.survey_id_p,surveyObj.get_survey_id());
            json.put(Constants.survey_name_p,surveyObj.get_survey_name());
            json.put(Constants.survey_title_p,surveyObj.get_survey_title());
            json.put(Constants.questions_num_p,surveyObj.get_questions_num());
            json.put(Constants.submitted_datetime_p,surveyObj.get_submitted_datetime());
            json.put(Constants.user_data_p,surveyObj.get_user_data());

            JSONArray arrQuestions = new JSONArray();
            JSONObject tempObj = null;
            for (QuestionObj item : surveyObj.get_questions_list()) {
                tempObj = new JSONObject();
                tempObj = CreateQuestionParamsJSON(item);
                arrQuestions.put(tempObj);
            }
            json.put(Constants.questions_list_p,arrQuestions);
            json.put(Constants.user_data_p, surveyObj.get_user_data());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public static JSONObject CreateSendDataParamsJSON(List<SurveyObj> AllSurveys, String senderID) {
        JSONObject json = new JSONObject();
        JSONObject senderJson = new JSONObject();
        JSONArray surveyRoot = new JSONArray();
        try {
            JSONArray arrSurveys = new JSONArray();
            JSONObject tempObj = null;
            for (SurveyObj item : AllSurveys) {
                tempObj = new JSONObject();
                tempObj = CreateSurveyParamsJSON(item);
                arrSurveys.put(tempObj);
            }
            json.put(Constants.sender_id_p, senderID);
            json.put(Constants.data_p,arrSurveys);

        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
